//
//  TNTeamFormViewController.m
//  跑跑快销
//
//  Created by ylife on 14-5-7.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "TNTeamFormViewController.h"
#import "TNTeamFormCell.h"
#import "YXCalendarView.h"
#import "YXMonthView.h"

@interface TNTeamFormViewController ()

@end

@implementation TNTeamFormViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"团队报表";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加右键
    UIBarButtonItem *dayItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"a.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(chooseDayCalendar)];
    UIBarButtonItem *monthItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"b.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(chooseMonthCalendar)];
    [self.navigationItem setRightBarButtonItems:@[monthItem,dayItem]];
    
    _isDay = YES;
    
    _teamFormView = [[TNTeamForm alloc]init];
    self.view = _teamFormView;
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *today = [NSDate date];
    NSString *datestring = [formatter stringFromDate:today];
    [_teamFormView.myDateButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [_teamFormView.myDateButton setTitle:datestring forState:UIControlStateNormal];
    [_teamFormView.myDateButton addTarget:self action:@selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside];
//    [_teamFormView.myDateButton setImage:[UIImage imageNamed:@"c.png"] forState:UIControlStateNormal];
    _teamFormView.tableView.dataSource = self;
    _teamFormView.tableView.delegate = self;

}

- (void)showCalendar:(UIButton *)btn
{
    if (_isDay) {
        if (!_dateBackButton) {
            _window = [[UIApplication sharedApplication].windows objectAtIndex:0];
            _dateBackButton = [UIButton buttonWithType:UIButtonTypeSystem];
            _dateBackButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
            _dateBackButton.frame = [UIScreen mainScreen].bounds;
            [_dateBackButton addTarget:self action:@selector(touchDayBackBtn) forControlEvents:UIControlEventTouchUpInside];
            [_window addSubview:_dateBackButton];
            
            YXCalendarView * dayCalendar = [[YXCalendarView alloc] initWithFrame:CGRectMake(0, btn.frame.origin.y + btn.frame.size.height + STARTY, 320, 325) withBlock:^(NSString * str) {
                [_teamFormView.myDateButton setTitle:str forState:UIControlStateNormal];
                _dateBackButton.hidden = YES;
            }];
            [_dateBackButton addSubview:dayCalendar];
        }
        [_dateBackButton setHidden:NO];
    }
    else{
        if (!_monthBackBtn) {
            UIWindow * window = [[UIApplication sharedApplication].windows objectAtIndex:0];
            _monthBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _monthBackBtn.frame = [UIScreen mainScreen].bounds;
            _monthBackBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
            [_monthBackBtn addTarget:self action:@selector(touchMonthBackBtn) forControlEvents:UIControlEventTouchUpInside];
            [window addSubview:_monthBackBtn];
            
            YXMonthView * monthView = [[YXMonthView alloc] initWithFrame:CGRectMake(0, btn.frame.origin.y + btn.frame.size.height + STARTY, 320, 110) withBlock:^(NSString * dateStr) {
                [_teamFormView.myDateButton setTitle:dateStr forState:UIControlStateNormal];
                _monthBackBtn.hidden = YES;
            }];
            [_monthBackBtn addSubview:monthView];
        }
        _monthBackBtn.hidden = NO;
    }
    
}

- (void)touchDayBackBtn
{
    [_dateBackButton setHidden:YES];
}

- (void)touchMonthBackBtn
{
    [_monthBackBtn setHidden:YES];
}

- (void)chooseDayCalendar
{
    _isDay = YES;
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    [_teamFormView.myDateButton setTitle:[formatter stringFromDate:[NSDate date]] forState:UIControlStateNormal];
    NSLog(@"day");
}

- (void)chooseMonthCalendar
{
    NSLog(@"month");
    _isDay = NO;
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
    [_teamFormView.myDateButton setTitle:[formatter stringFromDate:[NSDate date]] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 16;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TNTeamFormCell";
    TNTeamFormCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TNTeamFormCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}



@end
